package com.guigu.financial.controller;

import com.alibaba.excel.EasyExcel;
import com.financal.result.R;
import com.financal.result.ResponseEnum;
import com.guigu.financial.pojo.Dict;
import com.guigu.financial.pojo.dto.ExcelDictDTO;
import com.guigu.financial.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/admin/core/dict")
@Slf4j
@CrossOrigin
public class DictController {
    @Resource
    private DictService dictService;


    @PostMapping("/import")
    public R ImportExcelDict(@ApiParam(value = "Excel文件",required = true) @RequestParam("file")MultipartFile file){
        try{
            InputStream inputStream = file.getInputStream();
            dictService.ImportExcelDict(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.ok().message("数据字典批量导入成功");
    }

    @ApiOperation("Excel数据的导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response){

        try {
            // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("mydict", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ExcelDictDTO.class).sheet("数据字典").doWrite(dictService.listDictData());

        } catch (IOException e) {
            //EXPORT_DATA_ERROR(104, "数据导出失败"),
//            throw  new BusinessException(ResponseEnum.EXPORT_DATA_ERROR, e);
            e.printStackTrace();
        }
    }

    @ApiOperation("根据上级id获取子节点数据列表")
    @GetMapping("/listByParentId/{parentId}")
    public R listByParentId(
            @ApiParam(value = "上级节点id", required = true)
            @PathVariable Long parentId) {
        List<Dict> dictList = dictService.listByParentId(parentId);
        return R.ok().data("list", dictList);
    }
}
